<?php

namespace App\Http\Requests;

use App\User;
use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
//        dump($this->session());die;
        return [
            'name'=>'required|unique:users',
            'password'=>'required|confirmed',
            'account'=>[
                'required',
//                $value是传递到account里的值，及账号
                function($attribute,$value,$fail){
            if(filter_var($value,FILTER_VALIDATE_EMAIL)){
                $user=User::where('email','$value')->first();
                if($user){
                    return $fail('该邮箱已经注册');
                }
                return true;
            }else{
                $user=User::where('phone','$value')->first();
                if($user){
                    return $fail('该手机已经注册');
                }
                return true;
            }
                }
            ],
            'code'=>[
                'required',
                function ($attribute,$value,$fail){
            if($value !=session('valid_code.code')){
                return $fail('验证不正确');
            }
            return true;
                }
            ]
        ];
    }
    public function messages()
    {
        return [
            'name.required'=>'昵称 不能为空',
            'name.unique'=>'昵称 已经存在',
            'password.required'=>'密码 不能为空',
            'password.confirmed'=>'两次输入密码不一致',
            'account.required'=>'账号 不能为空',
            'code.required'=>'验证 码不能为空'
        ];
    }
}
